package  com.ruoyi.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeUtil {

	public static long oneDayMillisecond = 86400000;

	public static String formatYYYYMM(String fr) {
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
			Date parse = sf.parse(fr);
			String result = sf.format(parse);
			return result;
		} catch (Exception e) {
			return null;
		}
	}

	public static Date latterhour(Date date, int hour) {
		long currentTimeMillis = date.getTime();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = dateformat.format(3600000 * hour + currentTimeMillis);
		Date newdate;
		try {
			newdate = dateformat.parse(dateStr);
			return newdate;
		} catch (ParseException e) {
			return new Date();
		}
	}

	public static Date lattermins(Date date, int mins) {
		long currentTimeMillis = date.getTime();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = dateformat.format(60000 * mins + currentTimeMillis);
		Date newdate;
		try {
			newdate = dateformat.parse(dateStr);
			return newdate;
		} catch (ParseException e) {
			return new Date();
		}
	}

	/**
	 * 时间退后几分钟
	 * 
	 * @param mins
	 * @return
	 */
	public static Date lattermins(int mins) {
		Date date = new Date();
		long currentTimeMillis = date.getTime();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = dateformat.format(60000 * mins + currentTimeMillis);
		Date newdate;
		try {
			newdate = dateformat.parse(dateStr);
			return newdate;
		} catch (ParseException e) {
			return new Date();
		}
	}

	public static Date beforehour(Date date, int hour) {
		long currentTimeMillis = date.getTime();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = dateformat.format(currentTimeMillis - 3600000 * hour);
		Date newdate;
		try {
			newdate = dateformat.parse(dateStr);
			return newdate;
		} catch (ParseException e) {
			return new Date();
		}
	}

	public static Date todayStartTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	public static Date todayEndTime() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	/**
	 * 获取当前月份
	 * 
	 * @return
	 */
	public static String currentMonth() {
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
		String format = sf.format(new Date());
		return format;
	}

	public static long currentMillisecond() {
		return new Date().getTime();
	}

	/**
	 * 通过时间秒毫秒数判断两个时间的间隔
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDays(Date date) {
		int days = (int) ((new Date().getTime() - date.getTime()) / (1000 * 3600 * 24));
		return days;
	}

	public static String formetaDiff(Date date) {
		int days = differentDays(date);
		String res = "";
		int week = 7;
		if (days <= 1) {
			res = "近一天";
		} else if (days == 2) {
			res = "近两天";
		} else if (days == 3) {
			res = "近三天";
		} else if (days <= week) {
			res = "近一周";
		} else if (days > week && days <= week * 2) {
			res = "近两周";
		} else if (days > week * 2 && days <= week * 3) {
			res = "近三周";
		} else if (days > week * 3 && days <= week * 4) {
			res = "近一月";
		} else if (days > week * 4 && days <= week * 8) {
			res = "近两月";
		} else if (days > week * 8 && days <= week * 12) {
			res = "近两月";
		} else if (days > week * 12 && days <= week * 16) {
			res = "近三月";
		} else if (days > week * 16 && days <= week * 24) {
			res = "近半年";
		} else if (days > week * 24 && days <= week * 48) {
			res = "近一年";
		} else {
			res = "一年前";
		}
		return res;
	}

	/**
	 * 返回服务器时间 格式 2019.02.25 星期一
	 * 
	 * @return
	 */
	public static String currentDay() {
		Date date = new Date();
		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy.MM.dd  EEEE", Locale.CHINA);
		String currSun = dateFm.format(date);
		return currSun;
	}

	public static String currentTime() {
		Date date = new Date();
		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currSun = dateFm.format(date);
		return currSun;
	}

	public static void main(String[] args) {
		System.out.println(currentTime());
	}

	public static String formateDate(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currSun = dateFm.format(date);
		return currSun;
	}

	public static int getMonthSpace(String date1, String date2) throws ParseException {
		int result = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(sdf.parse(date1));
		c2.setTime(sdf.parse(date2));
		result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
		return result == 0 ? 1 : Math.abs(result);
	}

}
